package com.gotobject.junit.runner;

import com.gotobject.junit.internal.JUnitPerformanceTextListener;
import org.junit.runner.JUnitCore;
import org.junit.runner.notification.RunListener;

/**
 *
 * @author Huascar A. Sanchez (huascar.sanchez@gmail.com)
 */
public class Launcher {
	private final JUnitCore core;

    /**
	 * construct a {@link Launcher} of unit tests.
	 * @param core is the class responsible for running tests.
	 */
	Launcher(JUnitCore core){
		this.core = core;
	}

    /**
     * a method that executes a set of classes and outputs its results via
     * a user-defined {@link RunListener}.
     * @param listener user-defined {@link RunListener}.
     * @param classes to be executed.
     */
    public static void of(RunListener listener, Class<?>... classes){
        new Launcher(new JUnitCore()).with(listener).exec(classes);
    }

    /**
     * a method to be used in the case of performance tests when the assumed {@link RunListener}
     * is a {@link JUnitPerformanceTextListener}. If you do have your implementation of the
     * {@link RunListener} please {@link #of(RunListener, Class[])} instead.
     * @param classes to be executed.
     */
    public static void of(Class<?>... classes){
        of(new JUnitPerformanceTextListener(System.out), classes);
    }

    void exec(Class<?>... classes){
		core.run(classes);
	}

    Launcher with(RunListener listener){
        core.addListener(listener);
        return this;
    }
}
